# Maintainer: Martell Malone <martellmalone@gmail.com>

_realname=cling
pkgbase=mingw-w64-${_realname}-git
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-cern-git"
         "${MINGW_PACKAGE_PREFIX}-clang-cern-git"
         "${MINGW_PACKAGE_PREFIX}-clang-analyzer-cern-git"
         "${MINGW_PACKAGE_PREFIX}-llvm-cern-git")
pkgver=2518.6383df2
pkgrel=1
clingver=cling-patches-r214630
pkgdesc="LC language family frontend for LLVM (mingw-w64)"
arch=('any')
url="https://llvm.org/"
license=("custom:University of Illinois/NCSA Open Source License")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "python2" "patch" "groff")
depends=("${MINGW_PACKAGE_PREFIX}-gcc")
options=('staticlibs' 'strip')
source=("llvm-cern"::"git+http://root.cern.ch/git/llvm.git#tag=$clingver"
        "clang-cern"::"git+http://root.cern.ch/git/clang.git#tag=$clingver"
        "cling"::"git+http://root.cern.ch/git/cling.git"
        "0001-Add-SEH-exceptions-to-the-clang-frontend.patch"
        "0001-change-all-WIN32-defines-to-_WIN32-as-it-is-a-compil.patch"
        "0002-Invoke-bash-when-looking-for-gcc-includes.patch"
        "0003-use-size_t-for-void-pointer-cast.patch"
        "0004-Hack-around-missing-functions.patch")

sha256sums=('SKIP'
            'SKIP'
            'SKIP'
            '0dd7ae26c2dfdd4b7ed9ae4f90712ea5c530cd6a59a34efc951db5415c851d94'
            '9f6fb235834feeec2d2ab03bef81cd1dcafa37ef7553224a17045f045b60a24c'
            'feeacdfdf6586bf9dd43f64ba60746af52c468c7f7dca07b48453b28ebb2c6a8'
            '40f37bf1146cc17722b43b3b67a40824d4300305beb4e750e4f5fa057f012237'
            '25074103748b8f2ca531fcaddc24ec705c485649d3f0ea7cd672466cc1f6de9a')

pkgver() {
  cd "$srcdir/../cling/"
  local ver="$(git rev-parse HEAD)"
  printf "%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "${srcdir}/"
  cp -par ../cling ./
  cp -par ../clang-cern ./

  #cd ${srcdir}/clang
  #patch -p1 -i ${srcdir}/0001-Add-SEH-exceptions-to-the-clang-frontend.patch

  cd "${srcdir}/cling/"
  git checkout 39c7e56
  git am "${srcdir}/0001-change-all-WIN32-defines-to-_WIN32-as-it-is-a-compil.patch"
  git am "${srcdir}/0002-Invoke-bash-when-looking-for-gcc-includes.patch"
  git am "${srcdir}/0003-use-size_t-for-void-pointer-cast.patch"
  git am "${srcdir}/0004-Hack-around-missing-functions.patch"

  cd "${srcdir}"
  mv "${srcdir}/clang-cern" "${srcdir}/llvm-cern/tools/clang"
  mv "${srcdir}/cling" "${srcdir}/llvm-cern/tools/cling"
}

build() {
  mkdir -p "${srcdir}/llvm-cern/build-${MINGW_CHOST}"
  cd "${srcdir}/llvm-cern/build-${MINGW_CHOST}/"
  echo 'ac_cv_have_decl_strerror_s=${ac_cv_have_decl_strerror_s=no}' > config.cache

  # Include location of libffi headers in CPPFLAGS
  CPPFLAGS+=" $(pkg-config --cflags libffi)"

  ../configure \
    -C \
    --prefix="${MINGW_PREFIX}" \
    --build="${MINGW_CHOST}" \
    --host="${MINGW_CHOST}" \
    --with-sysroot="${MINGW_PREFIX}" \
    --enable-targets=x86,x86_64,cpp \
    --enable-optimized \
    --disable-assertions \
    --disable-pthreads \
    --with-python=/usr/bin/python2 \
    --enable-libffi \
    --enable-docs
  make
}

package_clang(){
  pkgdesc="C language family frontend for LLVM"
  depends=("${MINGW_PACKAGE_PREFIX}-llvm-cern-git")
  provides=("${MINGW_PACKAGE_PREFIX}-clang")
  conflicts=("${MINGW_PACKAGE_PREFIX}-clang")
  cd "${srcdir}/llvm-cern/tools/clang"
  local ver="$(git rev-parse HEAD)"
  pkgver="r$(git rev-list --count HEAD)"

  cd "${srcdir}/llvm-cern/build-${MINGW_CHOST}/tools/clang"
  make -j1 DESTDIR="$pkgdir" install
  rm -r "${pkgdir}${MINGW_PREFIX}/docs"
}

package_cling(){
  pkgdesc="C language family frontend for LLVM"
  depends=("${MINGW_PACKAGE_PREFIX}-llvm-cern-git" "${MINGW_PACKAGE_PREFIX}-clang-cern-git")
  provides=("${MINGW_PACKAGE_PREFIX}-cling")
  conflicts=("${MINGW_PACKAGE_PREFIX}-cling")
  cd "${srcdir}/llvm-cern/tools/cling"
  local ver="$(git rev-parse HEAD)"
  pkgver="r$(git rev-list --count HEAD)"

  cd "${srcdir}/llvm-cern/build-${MINGW_CHOST}/tools/cling"
  make -j1 DESTDIR="$pkgdir" install

  # map hack
  nm "${srcdir}/llvm-cern/build-${MINGW_CHOST}/Release/bin/cling.exe" > "${pkgdir}${MINGW_PREFIX}/bin/cling_func.map"
}


package_llvm(){
  pkgdesc="Low Level Virtual Machine"
  depends=("${MINGW_PACKAGE_PREFIX}-gcc")
  provides=("${MINGW_PACKAGE_PREFIX}-llvm")
  conflicts=("${MINGW_PACKAGE_PREFIX}-llvm")
  cd ${srcdir}/llvm-cern
  local ver="$(git rev-parse HEAD)"
  pkgver="r$(git rev-list --count HEAD)"

  mv "${srcdir}/llvm-cern/tools/clang" "${srcdir}/clang-cern"
  mv "${srcdir}/llvm-cern/build-${MINGW_CHOST}/tools/clang" "${srcdir}/clang-${MINGW_CHOST}"
  mv "${srcdir}/llvm-cern/tools/cling" "${srcdir}/cling"
  mv "${srcdir}/llvm-cern/build-${MINGW_CHOST}/tools/cling" "${srcdir}/cling-${MINGW_CHOST}"
  cd "${srcdir}/llvm-cern/build-${MINGW_CHOST}"
  make -j1 DESTDIR="$pkgdir" install
  mv "${srcdir}/clang-cern" "${srcdir}/llvm-cern/tools/clang"
  mv "${srcdir}/clang-${MINGW_CHOST}" "${srcdir}/llvm-cern/build-${MINGW_CHOST}/tools/clang"
  mv "${srcdir}/cling" "${srcdir}/llvm-cern/tools/cling"
  mv "${srcdir}/cling-${MINGW_CHOST}" "${srcdir}/llvm-cern/build-${MINGW_CHOST}/tools/cling"
  rm -r "${pkgdir}${MINGW_PREFIX}/docs"

  # Install CMake stuff
  install -d "${pkgdir}${MINGW_PREFIX}"/share/llvm/cmake/{modules,platforms}
  install -Dm644 $srcdir/llvm-cern/cmake/modules/*.cmake "${pkgdir}${MINGW_PREFIX}/share/llvm/cmake/modules/"
  install -Dm644 $srcdir/llvm-cern/cmake/platforms/*.cmake "${pkgdir}${MINGW_PREFIX}/share/llvm/cmake/platforms/"
}

package_clang-analyzer() {
  pkgdesc="A source code analysis framework"
  url="https://clang-analyzer.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-clang-cern-git" "${MINGW_PACKAGE_PREFIX}-python2")
  provides=("${MINGW_PACKAGE_PREFIX}-clang-analyzer")
  conflicts=("${MINGW_PACKAGE_PREFIX}-clang-analyzer")
  cd "${srcdir}/llvm-cern/tools/clang"
  local ver="$(git rev-parse HEAD)"
  pkgver="r$(git rev-list --count HEAD)"

  install -d ${pkgdir}${MINGW_PREFIX}/{bin,lib/clang-analyzer}
  for _tool in scan-{build,view}; do
    cp -r tools/$_tool ${pkgdir}${MINGW_PREFIX}/lib/clang-analyzer/
    ln -s ${pkgdir}${MINGW_PREFIX}/lib/clang-analyzer/$_tool/$_tool ${pkgdir}${MINGW_PREFIX}/bin/
  done

  # scan-build looks for clang within the same directory
  install -d ${pkgdir}${MINGW_PREFIX}/lib/clang-analyzer/scan-build/
  ln -s ${srcdir}/llvm-cern/build-${MINGW_CHOST}/Release/bin/clang.exe ${pkgdir}${MINGW_PREFIX}/lib/clang-analyzer/scan-build/

  # Relocate man page
  install -d ${pkgdir}${MINGW_PREFIX}/share/man/man1
  mv ${pkgdir}${MINGW_PREFIX}/lib/clang-analyzer/scan-build/scan-build.1 \
    ${pkgdir}${MINGW_PREFIX}/share/man/man1/

  # Compile Python scripts
  python2 -m compileall "${pkgdir}${MINGW_PREFIX}/lib/clang-analyzer"
  python2 -O -m compileall "${pkgdir}${MINGW_PREFIX}/lib/clang-analyzer"
  install -Dm644 LICENSE.TXT "${pkgdir}${MINGW_PREFIX}/share/licenses/clang-analyzer/LICENSE"
}

package_mingw-w64-i686-clang-cern-git(){
  package_clang
}

package_mingw-w64-i686-cling-cern-git(){
  package_cling
}

package_mingw-w64-i686-clang-analyzer-cern-git(){
  package_clang-analyzer
}

package_mingw-w64-i686-llvm-cern-git(){
  package_llvm
}

package_mingw-w64-x86_64-clang-cern-git(){
  package_clang
}

package_mingw-w64-x86_64-cling-cern-git(){
  package_cling
}

package_mingw-w64-x86_64-clang-analyzer-cern-git(){
  package_clang-analyzer
}

package_mingw-w64-x86_64-llvm-cern-git(){
  package_llvm
}
